package org.zero.onlinesubject.controller;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.zero.onlinesubject.common.param.LiveRoomParam;
import org.zero.onlinesubject.common.vo.LiveRoomVo;
import org.zero.onlinesubject.common.vo.Result;
import org.zero.onlinesubject.entity.LiveRoom;
import org.zero.onlinesubject.service.LiveRoomService;
import org.zero.onlinesubject.service.impl.ThreadService;
import org.zero.onlinesubject.utils.IDWorker;
import org.zero.onlinesubject.utils.UserContextHolder;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/live/api")
public class LiveController {

    @Resource
    private LiveRoomService liveRoomService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private ThreadService threadService;

    @GetMapping("/getBizId")
    public Result<String> createBizId(){
        return Result.ok(IDWorker.createLiveID());
    }

    // 创建直播间
    @PostMapping("/createLive")
    public Result<String> createLive(@RequestBody LiveRoomParam param){
        String startTime = param.getStartTime();
        if(StringUtils.isNullOrEmpty(startTime)){
            return Result.error(500,"开始时间不能为空！");
        }

        Boolean flag = liveRoomService.isTimeConflict(startTime);
        if(!flag){
            return Result.error(500,"当前时间的前后两小时，与其他直播时间冲突,请调整时间");
        }
        return liveRoomService.saveLive(param);
    }

    // 获取直播间信息
    @GetMapping("/liveInfo")
    public Result<LiveRoomVo> getLiveInfo(String liveId){
        if(StringUtils.isNullOrEmpty(liveId)){
            return Result.error(400,"房间号不能为空");
        }
        Result<LiveRoomVo> result = liveRoomService.getLiveInfo(liveId);
        LiveRoomVo liveRoom = result.getData();
        if(liveRoom==null){
            return Result.error(500,result.getMsg());
        }

        return Result.ok(liveRoom);
    }

    // 获取直播间列表
    @GetMapping("/liveList")
    public Result<List<LiveRoom>> getLiveList(String liveName,Integer current,Integer size){
        if(size == null || size == 0){
            size = 12;
        }
        if(current == null){
            current = 1;
        }
        current = (current - 1) * size;
        List<LiveRoom> liveRooms = liveRoomService.getLiveList(liveName,current,size);
        return Result.ok(liveRooms);
    }

    // 获取用户发布的直播
    @GetMapping("/ownLive")
    public Result<List<LiveRoom>> getOwnLive(){
        List<LiveRoom> rooms = liveRoomService.getOwnLive();
        return Result.ok(rooms);
    }

    // 更新直播状态
    @GetMapping("/updateLiveState")
    public Result<String> updateLiveState(String liveId,String liveState){
        UpdateWrapper<LiveRoom> wrapper = new UpdateWrapper<>();
        wrapper.eq("LiveID",liveId).set("LiveState",liveState).last(" limit 1");
        boolean flag = liveRoomService.update(wrapper);
        if(!flag){
            return Result.error(500,"状态更新失败");
        };
        threadService.updateLiveRoomCache(stringRedisTemplate,UserContextHolder.get().getUserid(),liveId,liveState);
        return Result.ok("状态更新成功");
    }

    @GetMapping("/updateViewCount")
    public Result<String> updateViewCount(String liveId){
        boolean flag = liveRoomService.updateViewCount(liveId);
        return flag ? Result.ok("更新观看次数成功"):Result.error(500,"更新观看次数失败");
    }

    @PostMapping("/changeLive")
    public Result<String> changeLive(String liveId,Boolean state){
        Boolean flag = liveRoomService.updateLive(liveId,state);
        return flag ? Result.ok("更新状态成功") : Result.error(500,"更新状态失败");
    }

    @PostMapping("/delLive")
    public Result<String> delLive(String liveId){
        Boolean flag = liveRoomService.deleteLive(liveId);
        return flag ? Result.ok("删除直播成功") : Result.error(500,"删除直播失败");
    }
}
